package com.shadowtorcher.stdisk.util;

import com.google.gson.Gson;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.TreeMap;

public class HttpResponser {
    protected Gson gson;
    protected TreeMap<String, Object> map;

    public HttpResponser() {
        gson = new Gson();
        map = new TreeMap<>();
    }

    public void put(String key, Object val) {
        map.put(key, val);
    }

    public Object get(String key) {
        return map.get(key);
    }

    public void setCode(int code) {
        map.put("code", code);
    }

    public void setMessage(String message) {
        map.put("message", message);
    }

    public void response(HttpServletResponse response) {
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/json");
            gson.toJson(map, response.getWriter());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String toString() {
        return map.toString();
    }
}
